/*
 * JBoss, Home of Professional Open Source
 *
 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
 * by the @author tags. See the COPYRIGHT.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.netty.handler.codec.bayeux;

/**
 * A Bayeux client initiates a connection negotiation by sending a message to
 * the "/meta/handshake" channel. For same domain connections, the Handshake
 * requests MUST be sent to the server as the 'message' parameter of an
 * application/x-www-form-urlencoded encoded POST request. For cross domain
 * connections, the Handshake request MUST be sent to the server as a url
 * encoded GET request with the jsonp parameter set for callback-polling.
 *
 * A handshake request MUST contain the message fields:
 *   channel
 *     value "/meta/handshake"
 *   version
 *     The version of the protocol supported by the client.
 *   supportedConnectionTypes
 *     An array of the connection types supported by the client for the purposes
 *     of the connection being negotiated. This list MAY be a subset of the
 *     connection types actually supported if the client wishes to negotiate a
 *     specific connection type.
 *
 * A handshake request MAY contain the message fields:
 *   minimumVersion
 *   ext
 *   id
 *
 * A client SHOULD NOT send any other message in the request with a handshake
 * message. A server MUST ignore any other message sent in the same request as
 * a handshake message. An example handshake request is:
 * [
 *   {
 *      "channel": "/meta/handshake",
 *      "version": "1.0",
 *      "minimumVersion": "1.0beta",
 *      "supportedConnectionTypes": ["long-polling", "callback-polling", "iframe"]
 *    }
 * ]

 * @author daijun
 */
public class HandshakeRequest extends BayeuxMessage implements BayeuxInterface {

    //requried
    //private String channel;
    //private String version;
    //private String[] supportedConnectionTypes;
    //optional
    //private String minimumVersion;
    //private String id;
    //private BayeuxExt ext;
    public HandshakeRequest(BayeuxMessage bayeuxMessage) {
        this.channel = bayeuxMessage.getChannel();
        this.version = bayeuxMessage.getVersion();
        this.supportedConnectionTypes = bayeuxMessage.getSupportedConnectionTypes();
        this.minimumVersion = bayeuxMessage.getMinimumVersion();
        this.id = bayeuxMessage.getId();
        this.ext = bayeuxMessage.getExt();
    }

    @Override
    public boolean isValid() {
        if (!channel.matches("/meta/handshake")) {
            return false;
        }
        if (version == null || version.length() == 0) {
            return false;
        }
        if (supportedConnectionTypes == null || supportedConnectionTypes.length == 0) {
            return false;
        }
        if (ext != null && !ext.isValid()) {
            return false;
        }
        return true;
    }

    static boolean isValid(BayeuxMessage bayeux) {
        return new HandshakeRequest(bayeux).isValid();
    }
}
